/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.fzhucc.modules.hot.service;

import com.fzhucc.modules.hot.domain.Hot;
import com.fzhucc.utils.ValidationUtil;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import lombok.RequiredArgsConstructor;
import com.fzhucc.modules.hot.repository.HotRepository;
import com.fzhucc.modules.hot.domain.dto.HotDto;
import com.fzhucc.modules.hot.domain.dto.HotQueryCriteria;
import com.fzhucc.modules.hot.service.mapstruct.HotMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author yyg
* @date 2024-01-26
**/
@Service
@RequiredArgsConstructor
public class HotService {

    private final HotRepository hotRepository;
    private final HotMapper hotMapper;

    public Map<String,Object> queryAll(HotQueryCriteria criteria, Pageable pageable){
        Page<Hot> page = hotRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(hotMapper::toDto));
    }

    public List<HotDto> queryAll(HotQueryCriteria criteria){
        return hotMapper.toDto(hotRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Transactional
    public HotDto findById(Integer id) {
        Hot hot = hotRepository.findById(id).orElseGet(Hot::new);
        ValidationUtil.isNull(hot.getId(),"Hot","id",id);
        return hotMapper.toDto(hot);
    }

    @Transactional(rollbackFor = Exception.class)
    public HotDto create(Hot resources) {
        return hotMapper.toDto(hotRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(Hot resources) {
        Hot hot = hotRepository.findById(resources.getId()).orElseGet(Hot::new);
        ValidationUtil.isNull( hot.getId(),"Hot","id",resources.getId());
        hot.copy(resources);
        hotRepository.save(hot);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            hotRepository.deleteById(id);
        }
    }

    public void download(List<HotDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (HotDto hot : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("福建省",  hot.getSheng());
            map.put("福州市",  hot.getFz());
            map.put("莆田市",  hot.getPt());
            map.put("泉州市",  hot.getQz());
            map.put("厦门市",  hot.getXm());
            map.put("漳州市",  hot.getZz());
            map.put("龙岩市",  hot.getLy());
            map.put("三明市",  hot.getSm());
            map.put("南平市",  hot.getNp());
            map.put("宁德市",  hot.getNd());
            map.put("平潭综合实验区",  hot.getPtang());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}